package com.devbobcorn.misaka.items;

import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Stats;
import net.minecraft.world.World;

public class CannedFoodItem extends Item {
    public CannedFoodItem(Properties settings) {
        super(settings);
    }

    @Override
    public ItemStack finishUsingItem(ItemStack stack, World world, LivingEntity user) {
        PlayerEntity player = user instanceof PlayerEntity ? (PlayerEntity) user : null;

        if (player instanceof ServerPlayerEntity) {
            CriteriaTriggers.CONSUME_ITEM.trigger((ServerPlayerEntity) player, stack);
        }

        // Consume and apply food item
        ItemStack result = this.isEdible() ? user.eat(world, stack) : stack;

        if (player != null) {
            player.awardStat(Stats.ITEM_USED.get(this));

            if (!player.abilities.instabuild && !stack.isEmpty()) {
                // Haven't yet finished eating this stack,
                // Give empty glass bottles to the player
                player.inventory.add(new ItemStack(Items.GLASS_BOTTLE));
            }
        }

        if (stack.isEmpty()) {
            // Finished eating this stack,
            // Turn into an empty bottle
            return new ItemStack(Items.GLASS_BOTTLE);
        } else {
            // Return the consumed itemstack
            return result;
        }
    }

}
